<?php


namespace app\admin\controller;


use app\common\controller\AdminController;
use app\common\model\AdminRole as roleModel;
use app\common\model\AdminRoleOperationRel;
use think\facade\View;

class AdminRole extends AdminController {

    public function index()
    {
        if($this->request->isAjax())
        {
            $name = $this->request->param('name','');
            $where_name = [];
            if($name){
                $where_name= 'name like "%'.$name.'%" ';
            }
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $roleModel = new roleModel();
            $res = $roleModel->where($where_name)
                ->paginate()
                ->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }
        return View::fetch();
    }


    /**
     * 添加
     * @return array|mixed
     */
    public function add()
    {
//        $result = [
//            'status' => false,
//            'msg' => error_code(10037,true),
//            'data' => ''
//        ];
        View::engine()->layout(false);
        $roleModel = new roleModel();
        if($this->request->isPost())
        {
            if(!input('?param.name'))
            {
                return error_code(11070);
            }

            $data['name'] = input('param.name');
            $roleModel->save($data);
            $result['status'] = true;
            $result['msg'] = '添加成功';
            $this->setActionLog('添加管理员角色',$this->admin_info['name'].'添加了管理员角色：'.$data['name']);
            return $result;
        }
        $result['status'] = true;
        $result['msg'] = '成功';
        $result['data'] = View::fetch();
        return $result;
    }

    /**
     * 删除
     * @return array|mixed
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        $id = $this->request->param('id');
        $model = new roleModel();
        $res = $model->where('id',$id)->delete();
        if($res) {
            $result['status'] = true;
            $result['msg'] = '删除成功';
            $this->setActionLog('删除管理员角色',$this->admin_info['name'].'删除了管理员角色：'.$id);
        }else{
            $result['msg'] = '删除失败，请联系管理员';
        }
        return $result;
    }


    /**
     * @return array|mixed|\think\Config
     */
    public function getOperation()
    {
        $return_data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [],
        ];

        if (!input('?param.id'))
        {
            return error_code(10000);
        }
        $roleModel = new roleModel();
        $re = $roleModel->getRoleOperation(input('param.id/d'),$this->admin_id);
        if(!$re['status'])
        {
            return $re;
        }
        $return_data['data'] = $re['data'];
        return $return_data;
    }


    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function savePerm()
    {
        $post = input('param.');
        if(!isset($post['id']))
        {
            return error_code(10000);
        }
        if(!isset($post['data']))
        {
            return error_code(11072);
        }
        //保存角色信息
        $roleModel = new roleModel();
        $roleInfo = $roleModel->where(['id'=>$post['id']])->find();
        if(!$roleInfo)
        {
            return error_code(11071);
        }
        $mrorModel = new AdminRoleOperationRel();
        $mrorModel->savePerm($post['id'],$post['data']);
        $this->setActionLog('修改角色权限',$this->admin_info['name'].'id:'.$post['id'].'data:'.json_encode($post['data']));
        return [
            'status' => true,
            'data' => '',
            'msg' => '设置成功'
        ];
    }




}